package com.ruoyi.zsyh.controller;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.zsyh.domain.ZsyhFactoryPay;
import com.ruoyi.zsyh.domain.ZsyhPartsSales;
import com.ruoyi.zsyh.service.IZsyhFactoryPayService;
import com.ruoyi.zsyh.service.IZsyhPartsSalesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhPartsRebate;
import com.ruoyi.zsyh.service.IZsyhPartsRebateService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品销量纪录Controller
 * 
 * @author yuexl
 * @date 2021-12-24
 */
@Controller
@RequestMapping("/zsyh/partsRebate")
public class ZsyhPartsRebateController extends BaseController
{
    private String prefix = "zsyh/partsRebate";

    @Autowired
    private IZsyhPartsRebateService zsyhPartsRebateService;
    @Autowired
    private IZsyhPartsSalesService zsyhPartsSalesService;

    @RequiresPermissions("zsyh:partsRebate:view")
    @GetMapping()
    public String partsRebate()
    {
        return prefix + "/partsRebate";
    }
    @Autowired
    private IZsyhFactoryPayService zsyhFactoryPayService;

    /**
     * 查询产品销量纪录列表
     */
    @RequiresPermissions("zsyh:partsRebate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhPartsRebate zsyhPartsRebate)
    {
        startPage();
        List<ZsyhPartsRebate> list = zsyhPartsRebateService.selectZsyhPartsRebateList(zsyhPartsRebate);
        return getDataTable(list);
    }

    /**
     * 查询产品销量纪录列表
     */
    @RequiresPermissions("zsyh:partsRebate:list")
    @PostMapping("/listView")
    @ResponseBody
    public TableDataInfo listView(ZsyhPartsRebate zsyhPartsRebate)
    {
        startPage();
        List<ZsyhPartsRebate> list = zsyhPartsRebateService.selectZsyhPartsRebateList(zsyhPartsRebate);
        return getDataTable(list);
    }

    /**
     * 导出产品销量纪录列表
     */
    @RequiresPermissions("zsyh:partsRebate:export")
    @Log(title = "产品销量纪录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhPartsRebate zsyhPartsRebate)
    {
        List<ZsyhPartsRebate> list = zsyhPartsRebateService.selectZsyhPartsRebateList(zsyhPartsRebate);
        ExcelUtil<ZsyhPartsRebate> util = new ExcelUtil<ZsyhPartsRebate>(ZsyhPartsRebate.class);
        return util.exportExcel(list, "产品销量纪录数据");
    }

    /**
     * 新增产品销量纪录
     */
    @GetMapping("/addOther")
    public String addOther()
    {
        return prefix + "/addOther";
    }

    /**
     * 新增产品销量纪录
     */
    @GetMapping("/addBySalesId")
    public String addBySalesId(String salesId, ModelMap mmap)
    {
        ZsyhPartsSales zsyhPartsSales = zsyhPartsSalesService.selectZsyhPartsSalesById(salesId);
        mmap.put("zsyhPartsSales",zsyhPartsSales);
        return prefix + "/add";
    }

    /**
     * 新增保存产品销量纪录
     */
    @RepeatSubmit
    @Transactional
    @RequiresPermissions("zsyh:partsRebate:add")
    @Log(title = "产品销量纪录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhPartsRebate zsyhPartsRebate)
    {
        ZsyhPartsSales zsyhPartsSales = new ZsyhPartsSales();
        if(EmptyUtils.isNotEmpty(zsyhPartsRebate.getId())){
            String salesId = zsyhPartsRebate.getId();
            zsyhPartsRebate.setSalesId(salesId);
            zsyhPartsSales.setId(salesId);
            zsyhPartsSales.setIsCheck("1");
            //修改销售统计单为核销状态
            zsyhPartsSalesService.updateZsyhPartsSales(zsyhPartsSales);
        }
        //设置创建人为当前登录人
        zsyhPartsRebate.setCreateId(ShiroUtils.getSysUser().getUserName());
        int num = zsyhPartsRebateService.insertZsyhPartsRebate(zsyhPartsRebate);
        if(num >0){
            //生成服务费记录
            //获取当年年月
            ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
            String dateString = DateUtils.getDate();
            Integer year = Integer.valueOf(dateString.substring(0,4));
            Integer month = Integer.valueOf(dateString.substring(5,7));
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month);
            zsyhFactoryPay.setFactoryId(zsyhPartsRebate.getFactoryId());
            List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayService.selectZsyhFactoryPayList(zsyhFactoryPay);
            //存在记录只用更新即可
            if(zsyhFactoryPayList.size()>0){
                ZsyhFactoryPay zsyhFactoryPay1 = zsyhFactoryPayList.get(0);
                zsyhFactoryPay1.setPayOn(zsyhFactoryPay1.getPayOn().add(zsyhPartsRebate.getRebateMoney()));
                zsyhFactoryPay1.setPayEnd(zsyhFactoryPay1.getPayEnd().add(zsyhPartsRebate.getRebateMoney()));
                zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay1);
            }else{
                zsyhFactoryPay.setPayBegin(new BigDecimal(0));
                zsyhFactoryPay.setPayOn(zsyhPartsRebate.getRebateMoney());
                zsyhFactoryPay.setPayOff(new BigDecimal(0));
                zsyhFactoryPay.setPayEnd(zsyhPartsRebate.getRebateMoney());
                zsyhFactoryPayService.insertZsyhFactoryPay(zsyhFactoryPay);
            }
        }
        return toAjax(num);
    }

    /**
     * 修改产品销量纪录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhPartsRebate zsyhPartsRebate = zsyhPartsRebateService.selectZsyhPartsRebateById(id);
        mmap.put("zsyhPartsRebate", zsyhPartsRebate);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品销量纪录
     */
    @RepeatSubmit
    @Transactional
    @RequiresPermissions("zsyh:partsRebate:edit")
    @Log(title = "产品销量纪录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhPartsRebate zsyhPartsRebate)
    {
        ZsyhPartsRebate oldZsyhPartsRebate = zsyhPartsRebateService.selectZsyhPartsRebateById(zsyhPartsRebate.getId());
        int num = zsyhPartsRebateService.updateZsyhPartsRebate(zsyhPartsRebate);
        if(num >0){
            //获取当年年月
            ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
            String dateString = DateUtils.getDate();
            Integer year = Integer.valueOf(dateString.substring(0,4));
            Integer month = Integer.valueOf(dateString.substring(5,7));
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month);
            zsyhFactoryPay.setFactoryId(oldZsyhPartsRebate.getFactoryId());
            List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayService.selectZsyhFactoryPayList(zsyhFactoryPay);
            //存在记录只用更新即可
            if(zsyhFactoryPayList.size()>0){
                ZsyhFactoryPay zsyhFactoryPay1 = zsyhFactoryPayList.get(0);
                zsyhFactoryPay1.setPayOn(zsyhFactoryPay1.getPayOn().add(zsyhPartsRebate.getRebateMoney().subtract(oldZsyhPartsRebate.getRebateMoney())));
                zsyhFactoryPay1.setPayEnd(zsyhFactoryPay1.getPayEnd().add(zsyhPartsRebate.getRebateMoney().subtract(oldZsyhPartsRebate.getRebateMoney())));
                zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay1);
            }else{
                return error("修改失败");
            }
        }
        return toAjax(num);
    }

    /**
     * 删除产品销量纪录
     */
    @Transactional
    @RequiresPermissions("zsyh:partsRebate:remove")
    @Log(title = "产品销量纪录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        ZsyhPartsRebate zsyhPartsRebate = zsyhPartsRebateService.selectZsyhPartsRebateById(ids);
        int num = zsyhPartsRebateService.deleteZsyhPartsRebateById(ids);
        if(num >0){
            //修改销售统计单为未核销
            if(EmptyUtils.isNotEmpty(zsyhPartsRebate.getSalesId())){
                ZsyhPartsSales zsyhPartsSales = new ZsyhPartsSales();
                zsyhPartsSales.setId(zsyhPartsRebate.getSalesId());
                zsyhPartsSales.setIsCheck("0");
                zsyhPartsSalesService.updateZsyhPartsSales(zsyhPartsSales);
            }
            //获取当年年月
            ZsyhFactoryPay zsyhFactoryPay = new ZsyhFactoryPay();
            String dateString = DateUtils.getDate();
            Integer year = Integer.valueOf(dateString.substring(0,4));
            Integer month = Integer.valueOf(dateString.substring(5,7));
            zsyhFactoryPay.setPeriodYear(year);
            zsyhFactoryPay.setPeriodMonth(month);
            zsyhFactoryPay.setFactoryId(zsyhPartsRebate.getFactoryId());
            List<ZsyhFactoryPay> zsyhFactoryPayList = zsyhFactoryPayService.selectZsyhFactoryPayList(zsyhFactoryPay);
            //存在记录只用更新即可
            if(zsyhFactoryPayList.size()>0){
                ZsyhFactoryPay zsyhFactoryPay1 = zsyhFactoryPayList.get(0);
                zsyhFactoryPay1.setPayOn(zsyhFactoryPay1.getPayOn().subtract(zsyhPartsRebate.getRebateMoney()));
                zsyhFactoryPay1.setPayEnd(zsyhFactoryPay1.getPayEnd().subtract(zsyhPartsRebate.getRebateMoney()));
                zsyhFactoryPayService.updateZsyhFactoryPay(zsyhFactoryPay1);
            }else{
                return error("修改失败");
            }
        }
        return toAjax(num);
    }
}
